package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.entity.RoleRightRelation;
import com.cloud.cmt.usermanage.entity.UserInfo;
import com.cloud.cmt.usermanage.po.TenantIdByUplinkPo;
import com.cloud.cmt.usermanage.service.RoleRightRelationRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/10/17
 * Time: 16:14
 */
@RestController
@RequestMapping("/role_right_relation")
@Api(value = "/赋值权限的模块，角色权限关联接口", description = "角色权限关联接口-权限增删查改等-by wcy")
public class RoleRightRelationCtl {

    @Resource
    RoleRightRelationRepository roleRightRelationRepository;

    @ApiOperation(value = "通过id获取信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public RoleRightRelation getUser(@PathVariable long id){
        return (RoleRightRelation) roleRightRelationRepository.findOne(id);
    }

    @ApiOperation(value = "保存信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<RoleRightRelation> save(@RequestBody RoleRightRelation RoleRightRelation){
        ResultMessage resultMessage = new ResultMessage();
        RoleRightRelation = roleRightRelationRepository.save(RoleRightRelation);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(RoleRightRelation.getId()+"");

        return resultMessage;
    }

    @ApiOperation(value = "更新信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<RoleRightRelation> update(@RequestBody RoleRightRelation RoleRightRelation){
        ResultMessage resultMessage = new ResultMessage();
        roleRightRelationRepository.saveAndFlush(RoleRightRelation);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }

    @ApiOperation(value = "树查询，根据角色查询权限id，和租户id获取子节点，顶级节点为-1")
    @PutMapping(value = "/findByTenantIdAndRoleId")
    @ResponseBody
    public List<RoleRightRelation> findByTenantIdAndRoleId(@RequestBody TenantIdByUplinkPo po){
        return roleRightRelationRepository.findByTenantIdAndRoleId(po.getTenantId(),po.getUplink());
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        roleRightRelationRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

}
